﻿using EasyBuyShop.DAL;
using EasyBuyShop.Models;
using Microsoft.AspNetCore.Mvc;

namespace EasyBuyShop.Controllers
{
    /// <summary>
    /// 购物车控制器
    /// </summary>
    public class CartController : Controller
    {
        /// <summary>
        /// 购物车列表页面
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            
            var userId = HttpContext.Session.GetInt32("userid");
            if (userId == null)
            {
               return Redirect("/Auth/login");
            }
            var cartDal = new CartDal();
            var productDal = new ProductDal();
            var cartList = cartDal.GetCartListByUserId((long)userId);
            var products = productDal.GetProductListByIds(cartList.Select(r => r.ProductId).ToList());
            ViewData["CartList"] = cartList;
            ViewData["ProductList"]= products;
            var username = HttpContext.Session.GetString("username");
            var realName = HttpContext.Session.GetString("realname");
            ViewData["Username"] = username;
            ViewData["RealName"] = realName;
            return View();
        }

        /// <summary>
        /// 首页或商品列表，快捷加入购物车
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Add(int productId)
        {
            Msg msg = new Msg();
            var userId = HttpContext.Session.GetInt32("userid");
            var userName= HttpContext.Session.GetString("username");
            if (userId == null)
            {
                msg.code = -1;
                msg.message = "尚未登录";
                return Json(msg);
            }
            var productDal = new ProductDal();
            var product = productDal.GetProduct(productId);
            if (product != null)
            {
                var cartDal = new CartDal();
                var cart=new Cart();
                cart.ProductId = productId;
                cart.CustomerId = userId.Value;
                cart.Quantity = 1;
                cart.Remark= string.Empty;
                cart.CreateUser = userName;
                cart.CreateTime=DateTime.Now;
                cart.LastEditUser = userName;
                cart.LastEditTime = DateTime.Now;
                int id = cartDal.InsertT<Cart>(cart);
                if(id > 0)
                {
                    msg.code = 0;
                    msg.message = "成功";
                    return Json(msg);
                }
                else
                {
                    msg.code = -1;
                    msg.message = "加入购物车失败";
                    return Json(msg);
                }
            }
            else
            {
                msg.code = -1;
                msg.message = "商品不存在";
                return Json(msg);
            }
            
        }

        /// <summary>
        /// 商品详情页面加入购物车
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddWithForm()
        {
            Msg msg = new Msg();
            var userId = HttpContext.Session.GetInt32("userid");
            var userName = HttpContext.Session.GetString("username");
            if (userId == null)
            {
                msg.code = -1;
                msg.message = "尚未登录";
                return Json(msg);
            }
            var productId =long.Parse( Request.Form["productId"]);
            var quantity = int.Parse(Request.Form["quantity"]);
            var color = Request.Form["color"];
            var size = Request.Form["size"];
            var remark = $"颜色：{color},大小：{size}";
            var productDal = new ProductDal();
            var product = productDal.GetProduct(productId);
            if (product != null)
            {
                var cartDal = new CartDal();
                var cart = new Cart();
                cart.ProductId = productId;
                cart.CustomerId = userId.Value;
                cart.Quantity = quantity;
                cart.Remark = remark;
                cart.CreateUser = userName;
                cart.CreateTime = DateTime.Now;
                cart.LastEditUser = userName;
                cart.LastEditTime = DateTime.Now;
                int id = cartDal.InsertT<Cart>(cart);
                if (id > 0)
                {
                    msg.code = 0;
                    msg.message = "成功";
                }
                else
                {
                    msg.code = -1;
                    msg.message = "加入购物车失败";
                }
            }
            else
            {
                msg.code = -1;
                msg.message = "商品不存在";
            }
            return Redirect("/Cart/Index");

        }

        /// <summary>
        /// 移除购物车
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ActionResult Delete(int Id)
        {
            var cartDal =new CartDal();
            if(cartDal.DeleteById(Id) > 0)
            {
                //成功
            }
            else
            {
                //删除失败
            }
            return View();
        }
    }
}
